import request from '@/utils/request';
import {UserCreateParam, UserToggleParam, UserRoleSetParam, PasswordSetParam} from "@/pages/UsersMng/data";

export async function queryUsers() {
  return request('/api/admin/users')
}

export async function toggleUser(params: UserToggleParam) {
  const newParams = {
    id: params.id,
  }
  return params.isEnabled ?
  request("/api/admin/user/disable", {
    method: 'POST',
    data: {
      ...newParams
    },
  }) :
  request("/api/admin/user/enable", {
    method: 'POST',
    data: {
      ...newParams
    },
  });
}


export async function addUser(param: UserCreateParam) {
  return request('/api/admin/user/add', {
    method: 'POST',
    data: {
      ...param
    },
  })
}

export async function modifyUser(params: UserRoleSetParam) {
  const newParams = {
    id: params.id,
    newRole: params.newRole,
  }
  return request('/api/admin/user/role', {
    method: 'POST',
    data: {...newParams},
  })
}

export async function setPassword(params: PasswordSetParam) {
  const newParams = {
    id: params.id,
    newPassword: params.newPassword,
  }
  return request('/api/admin/user/password', {
    method: 'POST',
    data: {...newParams}
  })
}
